// Juan Carlos Leon
// CSC136 LAB
// Prof. Roberto Jimeno.
//    Tools from Board:    
//        - char get(int X, int Y); get the character in position X,Y
//        - void put(char ch, int X, int Y); places ch in position X,Y 
//        - void del(int X, int Y); delete the character in position X,Y
//        - char getBackgroundCh(); returns the default background character
//        - bool isEmpty(int X, int Y); returns true if position X,Y is empty
//          (that is it contains a character that is equal to the default brackground char)
//        - bool isOccupied(int X, int Y); returns true if position X,Y is NOT empty
//          (that is it contains a character that is equal to something OTHER than the default background char
//           Note: if X,Y is off the board, isOccupied returns false)   
//        - void fillBoard(char ch); fill all positions on the board with char ch. 
//        - void clearBoard(); clears the board (overwrites all the cells with the default background character). 
//        - void wait( int seconds ); 
//        -   
//**********************************************************************************
#include "Board.h" // include definitions of class Board
using namespace std;
int size = 20; 

bool neighbors( int row, int column, Board &matrix ) { 

    if ( row < 0 ) { 
        // set row to matrix.size()-1 which is the other side in the matrix.
        // if out of boundary. 
        row = size-1; 
    } else if ( row >= size ) { 
        // ohterwise is the first elment
        row = 0; 
    }

    if ( column < 0 ) { 
        // set column to matrix.size() which is the other side in the matrix.
        // if out of boundary.
        column = size-1;   
    } else if (column >= size ) {
        // ohterwise is the first elment
        column = 0; 
    }

    return matrix.isOccupied(row,column);
} 

Board nextgen( Board &matrix) { 

    Board GameBoard2( size, size,' '); 

    int south, north, east, west; 
    int north_east, south_east; 
    int north_west, south_west;  
    int row; 
    int column = 0;  
    int counter = 0; 
    int vecinos = 0; 

    cout << endl;

    for(row=0; row < size-1; row++) {
        for(column=0; column < size-1; column++) {
                 north = neighbors(row-1, column, matrix) );
                 south = neighbors(row + 1, column, matrix);    
                 east  = neighbors(row, column+1, matrix);  
                  west = neighbors(row, column-1, matrix);  
            north_east = neighbors(row-1, column+1, matrix);    
            north_west = neighbors(row-1, column-1, matrix);    
            south_east = neighbors(row+1, column+1, matrix);   
            south_west = neighbors(row+1, column-1, matrix);   

            vecinos = north+south+east+west+north_east+north_west+south_east+south_west; 
            cout << "vecinos: " << vecinos << endl; 


 
            /*if (matrix.isOccupied(row,column) && vecinos < 2) {
                GameBoard2.put(' ', row, column); 
            } else if (matrix.isOccupied(row,column) && vecinos > 3) { 
                GameBoard2.put(' ',row, column);
            } else if (matrix.isOccupied(row, column) && (vecinos == 2 || vecinos == 3)) { 
                GameBoard2.put('X', row, column);
            } else if (matrix.isOccupied(row,column) && vecinos == 3) { 
                GameBoard2.put('X', row, column);
            }*/

            
  
        }

    }

    return GameBoard2; 
}  

int main() { 

    int generation = 20; 

    int x,y;

    // Initialize our Board  
    Board InitGen( size, size,' '); 

    srand( static_cast<unsigned int>( time(0) ));

    for( int xy=1; xy <= size; xy++) {
        x = rand() % generation; 
        y = rand() % generation; 
        InitGen.put('X', x, y);
    }

    cout << InitGen; 
    InitGen = nextgen( InitGen ); 
    cout << InitGen; 

    /*while (true) {
        InitGen = nextgen( InitGen );
        cout << InitGen;
        wait(1);
    }*/

    return 0;
}
